Soru & Cevap

SQLiteDatabase error “sqlite: (1) near ”)“: syntax error”

23.10.2015 - 02:07

Android programlamada yeniyim bir program yapmaya çalışıyorum. Programı similasyonda çalıştırdığımda hata alıyorum "SQLiteDatabase error “sqlite: (1) near ”)“: syntax error”".kodları buraya ekliyorum.

---->siparisekle.java<----------

import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.sql.SQLException;

import static android.R.*;


public class siparisekle extends Activity {


    private AutoCompleteTextView act1, act2, act3;

    private  EditText edittxt1;

    EditText etName,etPass;
    TextView tvResult;
    Button bSave;
    Button bShow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_siparisekle);

        install_elements();

        final Database db = new Database(this);


        bSave.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                boolean ok = true;
                String name = etName.getText().toString();
                String pass = etPass.getText().toString();

                try
                {
                  db.open();
                    db.addThat(name, pass);
                    tvResult.setText(name + "Eklendi");
                    db.close();

                }

                catch (Exception e)
                {
                    ok =false;
                    e.printStackTrace();
                    tvResult.setText("Sorun var");

                }

                finally
                {

                    if(ok)
                    {
                        Dialog d =new Dialog(siparisekle.this);
                        TextView tv = new TextView(siparisekle.this);
                        tv.setText("BASARILI");
                        d.setTitle("Sonuç");
                        d.setContentView(tv);
                        d.show();
                    }
                }
            }
        });
}
protected void install_elements()
{

    etName = (EditText) findViewById(R.id.etName);
    etPass= (EditText) findViewById(R.id.etPass);
    tvResult= (TextView) findViewById(R.id.tvResult);
    bSave= (Button) findViewById(R.id.bSave);
    bShow= (Button) findViewById(R.id.bShow);

}    
}

-------------------->Database.java<-----------------------------

package com.example.dogruprint.dogruprint2;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.sql.SQLException;


public class Database {


    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "u_name";
    public static final String KEY_PASS = "u_pass";

    private static final String DB_NAME = "app";
    private static final String DB_TABLE = "users";
    private static final int version = 1;


    private SQLiteDatabase ourDatabase;
    private DBHelper ourHelper;
    private Context ourContext;
    private SQLiteDatabase writableDatabase;


    public Database(Context context) {

        ourContext = context;

    }

    public SQLiteDatabase getWritableDatabase() {
        return writableDatabase;
    }


    public class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context)

        {
            super(context, DB_NAME, null, version);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {

          /* db.execSQL("CREATE TABLE" + DB_TABLE + "("
                    + KEY_ROWID + "(INTEGER PRIMARY KEY AUTOINCREMENT ,"
                    + KEY_NAME + "TEXT NOT NULL,"
                    + KEY_PASS + "TEXT NOT NULL);");
*/
             String CREATE_USERS_TABLE = "CREATE TABLE " + DB_TABLE + "(" + KEY_ROWID +
                    " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PASS + " TEXT,"
                     + ");";


            db.execSQL(CREATE_USERS_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST" + DB_TABLE);


        }


    }

    public Database open() throws SQLException {


        ourHelper = new DBHelper(ourContext);

        ourDatabase = ourHelper.getWritableDatabase();

        return this;


    }

    public Database close() {

        ourHelper.close();
        return this;


    }

    public void addThat(String name, String pass) {


        ContentValues cv = new ContentValues();

        cv.put(KEY_NAME, name);
        cv.put(KEY_PASS, pass);
        ourDatabase.insert(DB_TABLE, null, cv);


    }
}

536 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user timucinkursunlugil
timucinkursunlugil
24.10.2015 - 12:45

Yardımınız için teşekkür ederim sorunu farklı bir yolla çözdüm fakat kayıtları geri çağırmada şöyle bir sorun yaşıyorum : 

10091-10091/com.example.dogruprint.dogruprint2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such table: users (code 1): , while compiling: SELECT _id, u_name, u_pass FROM users
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
            at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
            at com.example.dogruprint.dogruprint2.Database.getThat(Database.java:123)
            at com.example.dogruprint.dogruprint2.siparisekle$2.onClick(siparisekle.java:113)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

düzeltme yaptığım database kodum :

######################### DATABASE ##############################

package com.example.dogruprint.dogruprint2;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.sql.SQLException;


public class Database {


    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "u_name";
    public static final String KEY_PASS = "u_pass";

    private static final String DB_NAME = "app";
    private static final String DB_TABLE = "users";
    private static final int version = 1;


    private SQLiteDatabase ourDatabase;
    private DBHelper ourHelper;
    private Context ourContext;
    private SQLiteDatabase writableDatabase;


    public Database(Context context) {

        ourContext = context;

    }

    public SQLiteDatabase getWritableDatabase() {
        return writableDatabase;
    }




    public class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context)

        {
            super(context, DB_NAME, null, version);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {


/*

            final String CREATE_TABLE = " CREATE TABLE " + DB_TABLE + " ( " + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                    + KEY_NAME + " VARCHAR(255) , "
                    + KEY_PASS + " VARCHAR(255) ,"
                   ;

*/


            String CREATE_USERS_TABLE = "CREATE TABLE " + DB_TABLE + "(" + KEY_ROWID +
                    " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PASS + " TEXT"
                    + ");";



        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST " + DB_TABLE);


        }


    }

    public Database open() throws SQLException {


        ourHelper = new DBHelper(ourContext);

        ourDatabase = ourHelper.getWritableDatabase();

        return this;


    }

    public Database close() {

        ourHelper.close();
        return this;


    }

    public void addThat(String name, String pass) {


        ContentValues cv = new ContentValues();

        cv.put(KEY_NAME, name);
        cv.put(KEY_PASS, pass);
        ourDatabase.insert(DB_TABLE, null, cv);


    }

    public String getThat()
    {
        String[] colomns = new String[]{KEY_ROWID,KEY_NAME,KEY_PASS};

        Cursor c = ourDatabase.query(DB_TABLE,colomns,null,null,null,null,null);
        int İROW = c.getColumnIndex(KEY_ROWID);
        int İNAME= c.getColumnIndex(KEY_NAME);
        int İPASS = c.getColumnIndex(KEY_PASS);

        String result = "";

        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
        {
            result = result + "name" + c.getString(İNAME) + "pass" + c.getString(İPASS)+"\n";


        }



        return result;
    }
}

#####################  siparisekle.java ##############################

package com.example.dogruprint.dogruprint2;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.sql.SQLException;

import static android.R.*;


public class siparisekle extends Activity {



    private AutoCompleteTextView act1, act2, act3;

    private  EditText edittxt1;

    EditText etName,etPass;
    TextView tvResult;
    Button bSave;
    Button bShow;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_siparisekle);

        final Database db = new Database(this);




        install_elements();





        bSave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean ok = true;
                String name = etName.getText().toString();
                String pass = etPass.getText().toString();

                try {
                    db.open();
                    db.addThat(name, pass);
                    tvResult.setText(name + "Eklendi");
                    db.close();

                } catch (Exception e) {
                    ok = false;
                    e.printStackTrace();
                    tvResult.setText("Sorun var");


                } finally {

                    if (ok) {
                        Dialog d = new Dialog(siparisekle.this);
                        TextView tv = new TextView(siparisekle.this);
                        tv.setText("BASARILI");
                        d.setTitle("Sonuç");
                        d.setContentView(tv);
                        d.show();


                    }

                }


            }
        });

        bShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)


            {
                try {
                    db.open();
                    String data = db.getThat();
                    tvResult.setText(data);
                    db.close();


                } catch (SQLException e)
                {

                    Toast.makeText(getApplicationContext(),"Çağırmada Bir Sorun Oluştu",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }


            }
        });






}
protected void install_elements()
{

    etName = (EditText) findViewById(R.id.etName);
    etPass= (EditText) findViewById(R.id.etPass);
    tvResult= (TextView) findViewById(R.id.tvResult);
    bSave= (Button) findViewById(R.id.bSave);
    bShow= (Button) findViewById(R.id.bShow);

}



}




 

 

picture-7541-1521024507.jpg
burhanaras
23.10.2015 - 06:25

Dostum;

Birincisi, küçük harfle başlayan class olmaz. Lütfen yazdığın sınıf adını SiparisEkle.java şeklinde düzelt. Çok çirkin durur bu şekilde class isimleri.

Sorununa gelince, SQL sorgun yanlış. Fazladan bir virgül var. Aşağıdaki SQL sorgusunu kullan onCreate(SQLiteDatabase db) metodu içinde.

     String CREATE_USERS_TABLE = "CREATE TABLE " + DB_TABLE + "(" + KEY_ROWID +
                    " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PASS + " TEXT"
                     + ");";

Başarılar dilerim.